The Binomial distribution visualize the Binomial distribution X~B(n,p) think of n coin tosses, sum number of "face"
In [1]:
options(repr.plot.width=10, repr.plot.height=6.5) # this command just formats the size of the figures. Adapt to view them nicely
# in your browser.
In [2]:
n <- 50
p <- 0.80
k <- seq(0,n,by=1) #Binomial: x can take any value k between 0 and n.
f_k <- dbinom(x = k,size = n,prob = p) # compute pdf of B(n,k) with dbinom
plot(k,f_k, ylab = "PDF")
In [3]:
# Show numerical pdf
f_k
- 1.12589990684261e-35
- 2.25179981368521e-33
- 2.20676381741155e-31
- 1.41232884314337e-29
- 6.63794556277396e-28
- 2.44276396710079e-26
- 7.32829190130244e-25
- 1.84254196375602e-23
- 3.96146522207543e-22
- 7.39473508120754e-21
- 1.21273655331804e-19
- 1.76398044118988e-18
- 2.29317457354682e-17
- 2.68125027060861e-16
- 2.83446457178623e-15
- 2.72108598891478e-14
- 2.38095024030043e-13
- 1.90476019224034e-12
- 1.39682414097625e-11
- 9.41018368657688e-11
- 5.83431388567769e-10
- 3.33389364895867e-09
- 1.75787119672365e-08
- 8.56006843621956e-08
- 3.85203079629882e-07
- 1.6024448112603e-06
- 6.1632492740781e-06
- 2.19137751967221e-05
- 7.20024042178015e-05
- 0.000218490054178156
- 0.000611772151698834
- 0.00157876684309377
- 0.0037495712523477
- 0.00818088273239499
- 0.01636176546479
- 0.0299186568499017
- 0.0498644280831694
- 0.0754704857474997
- 0.10327540154921
- 0.127108186522105
- 0.139819005174315
- 0.136408785535918
- 0.116921816173644
- 0.0870115841292231
- 0.0553710080822329
- 0.0295312043105242
- 0.012839654048054
- 0.00437094605891201
- 0.001092736514728
- 0.000178405961588244
- 1.42724769270596e-05
In [4]:
# Normal distribution
mean <- 5 # mu in literature
sd <- 0.8 # sigma
x <- seq(-5,10,length=1000)
y <- dnorm(x,mean,sd) #dnorm : "D"ensity of "NORM"al distribution. This gives the PDF.
plot(x, y, type="n", xlab="Parameter X", ylab="",
main="Normal Distribution", axes=FALSE)
axis(1, at=seq(0, 10, 2), pos=0)
lines(x, y)
In [5]:
# Normal distribution
mean=100
sd=15
lb=80 # lower bound of X
ub=120 # upper bound of x
#lb=mean-1*sd; ub=mean+1*sd
# This will compute the probability for lb < X < ub
x <- seq(-4,4,length=100)*sd + mean
hx <- dnorm(x,mean,sd) #dnorm : "D"ensity of "NORM"al distribution
plot(x, hx, type="n", xlab="Parameter X", ylab="",
main="Normal Distribution", axes=FALSE)
i <- x >= lb & x <= ub
lines(x, hx)
polygon(c(lb,x[i],ub), c(0,hx[i],0), col="red")
area <- pnorm(ub, mean, sd) - pnorm(lb, mean, sd) #pnorm : "P"robability of "NORM" (=Phi(Z))
result <- paste("P(",lb,"< X <",ub,") =",
signif(area, digits=4))
mtext(result,3)
axis(1, at=seq(40, 160, 20), pos=0)
In [6]:
# The Whiskey case
1-pnorm(q = 69.5,mean = 67.5,sd = 2.5)
0.211855398583397
In [ ]: